package com.qf.controller;


import com.qf.pojo.DtsAd;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CatgoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("admin/brand")
public class AdminBrandController {
    @Autowired
    BrandService brandService;

    @Autowired
    AdminService adminService;

    @Autowired
    CatgoryService catgoryService;


    @RequestMapping("/list")
    public Object getList(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                          @RequestParam(value = "limit",required = false,defaultValue = "3") Integer limit,
                          @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                          @RequestParam(value = "order",required = false,defaultValue = "desc") String order,Integer id,
                          String name) throws Exception {

        Subject subject = SecurityUtils.getSubject();


        System.out.println(id);
        List<Integer> list = new ArrayList<>();
        List<Integer> brandId = null;
        if(subject.hasRole("品牌制造商")){
            DtsAdmin admin = (DtsAdmin)subject.getPrincipal();
           brandId = brandService.findByAdminId(admin.getId());
        }
        if(brandId!=null && id!=null){
            for(int i=0;i<brandId.size();i++){
                Integer integer = (Integer)brandId.get(i);
                if(integer.equals(id)){

                    break;
                }
            }
            throw new Exception("您不能查询这个用户Id");
        }


        Map<String,Object> map = brandService.getPage(page,limit,sort,order,brandId,name,id);




        return ResponseUtil.ok(map);
    }

    @RequestMapping("/catAndAdmin")
    public Object getCatAndAdmin(){

       List<DtsAdmin> list =  adminService.selectAdmin();

      List<Map<String,Object>> list1 = new ArrayList<>();
       for(int i=0;i<list.size();i++){
           Map<String,Object> map = new HashMap<>();
           DtsAdmin admin = (DtsAdmin)list.get(i);
           map.put("value",admin.getId());
           map.put("label",admin.getUsername());
          list1.add(map);
       }

       List<CatVo> list2 = catgoryService.selectlist();

       Map map = new HashMap();
       map.put("adminList",list1);
       map.put("categoryList",list2);

       return ResponseUtil.ok(map);

    }

    @RequestMapping("/create")
    public Object creat(@RequestBody DtsBrand dtsBrand){

        dtsBrand.setAddTime(new Date());
        brandService.create(dtsBrand);

        return ResponseUtil.ok(dtsBrand);


    }

    @RequestMapping("/delete")
    public Object deleteBrand(@RequestBody DtsBrand brand){

        brandService.delete(brand);
        return ResponseUtil.ok();
    }

    @RequestMapping("/update")
    public Object uodate(@RequestBody DtsBrand dtsBrand){
        brandService.update(dtsBrand);

        return ResponseUtil.ok(dtsBrand);

    }





















}
